package oracle.apps.financials.collection.listener;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import oracle.apps.financials.collection.R;
import oracle.apps.financials.collection.adapter.ItemAdapter;
import oracle.apps.financials.collection.po.Collector;
import oracle.apps.financials.collection.po.Customer;
import oracle.apps.financials.collection.po.DetailItem;
import oracle.apps.financials.collection.po.Item;
import oracle.apps.financials.collection.util.MockDataFactory;
import android.R.color;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.PopupWindow;

public class CollectorPopupWindowListener extends BaseOnClickListener {

	private String initialQuery;
	private EditText mSearchEdit;
	private ItemAdapter adapter;
	private View layout;

	private View.OnKeyListener enterKeyHandler = new View.OnKeyListener() {
		public boolean onKey(View v, int keyCode, KeyEvent event) {
			if (keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
				if (event.getAction() == KeyEvent.ACTION_UP) {
					startSearch();
				}
				return true;
			}
			return false;
		}
	};

	public CollectorPopupWindowListener(Context context, Customer customer, Handler mHandler) {
		super(context, R.layout.collector_list, customer);
		this.mHandler = mHandler;
	}

	public void initPopupWindow() {

		popupWindow = new PopupWindow(popupWindowView, 250, 335);

		ListView listView = (ListView) popupWindowView.findViewById(R.id.collector_list);

		adapter = new ItemAdapter(context, getItemList(MockDataFactory.getCollectorList(initialQuery)));

		listView.setAdapter(adapter);

		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				ListView listView = (ListView) parent;
				DetailItem item = (DetailItem) listView.getItemAtPosition(position);
				Collector collector = MockDataFactory.getCollectorByName(item.text);
				customer.setCollector(collector);
				item.setHighlight(true);
				CollectorPopupWindowListener.this.notifyHandler(customer);
			}

		});

		mSearchEdit = (EditText) this.popupWindowView.findViewById(R.id.search_edit);
		mSearchEdit.setOnKeyListener(enterKeyHandler);
		mSearchEdit.addTextChangedListener(new TextWatcher() {

			@Override
			public void afterTextChanged(Editable arg0) {
				startSearch();
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub

			}

		});

		ImageButton searchButton = (ImageButton) popupWindowView.findViewById(R.id.search_button);

		searchButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				startSearch();
			}
		});

		popupWindow.setFocusable(true);
		popupWindow.setOutsideTouchable(true);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
	}

	protected boolean startSearch() {
		initialQuery = mSearchEdit.getText().toString();
		Log.d(TAG, "The initialQuery:" + initialQuery);
		adapter.resetAdapter(getItemList(MockDataFactory.getCollectorList(initialQuery)));
		return false;
	}

	public List<Item> getItemList(List<Collector> list) {
		List<Item> items = new ArrayList<Item>();
		Iterator<Collector> it = list.iterator();
		while (it.hasNext()) {
			Collector collector = it.next();
			DetailItem item = new DetailItem(collector.getCollectorName(), "CEI: " + collector.getCEI() + "  Customer Assigned: "
					+ collector.getCustomerList().size(), color.black, new CollectorDetailWindowListener(context, this, collector));
			
			items.add(item);
		}
		return items;
	}

	@Override
	public void onClick(View parent) {
		layout = parent;
		if (popupWindow == null) {
			initPopupWindow();
		}
		popupWindow.showAsDropDown(parent, 78, -33);
	}

	public View getLayout() {
		return layout;
	}

	public void setLayout(View layout) {
		this.layout = layout;
	}

}
